#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

using namespace sf;

int point = 0;

float offsetX = 0, offsetY = 0;

const int H = 12;
const int W = 50;

String TileMap[H] =
{	
	"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
	"B                                B               B",
	"B                                B               B",
	"B                                B  1111         B",
	"B                                B  SSSS         B",
	"B                              2 B           S   B",
	"B         0000                SSSB         SSSS  B",
	"B         SSS          2222      B        SS     B",
	"BSS                     2SSSSS   B  SSS          B",
	"B  S           BBB  SSS                  S       B",
	"B   S         BBBB                               B",
	"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",												          
};

class MONETA
{
	public:
		int dvi;
		Sprite s;

		MONETA(Texture &image)
		{
			s.setTexture(image);
			s.setTextureRect(IntRect(0, 0, 30, 30));
			dvi = 0;
		}
		
		MONETA(MONETA &m)
		{
			s = m.s;
			dvi = m.dvi;
		}

		void update(float time)
		{
			dvi += 0.060 * time;
			if (dvi>6) dvi -= 6;
			s.setTextureRect(IntRect(30 * int(dvi), 0, 30, 30));
		}
};

class CHEST
{
public:
	int dvi;
	FloatRect rect;
	Sprite s;
	CHEST (Texture &image)
	{
		s.setTexture(image);
		s.setTextureRect(IntRect(0, 0, 30, 30));
		rect = FloatRect(0, 0, 30, 30);
		dvi = 0;
	}
	CHEST(CHEST &s1)
	{
		s = s1.s;
		dvi = s1.dvi;
	}
	void update (float time)
	{
		dvi+=0.060 * time;
		if(dvi>5)dvi-=6;
		s.setTextureRect(IntRect(30*int(dvi),0,30,30));
		
	}
};

class PLAYER
{
	public:
		float dx, dy;
		FloatRect rect;
		bool onGround;
		Sprite s;
		float dvi;

		PLAYER(Texture &image)
		{
			s.setTexture(image);
			s.setTextureRect(IntRect(0, 0, 45, 48));
			rect=FloatRect(2 * 32, 4 * 32, 45, 48);
			dx = dy = 0;
			dvi = 0;
		}

		void update(float time)
		{
			rect.left += dx*time;
			
			Collision(0);

			if(!onGround) dy = dy + 0.0007 * time;
			rect.top += dy*time;
			onGround = false;

			Collision(1);

			dvi += 0.005 * time;

			if (dvi > 6) dvi -= 6;
			if (dx > 0) s.setTextureRect(IntRect((45 * int(dvi)), 0, 45, 48));
			if (dx < 0) s.setTextureRect(IntRect((45 * int(dvi)) + 45, 0, -45, 48));

			s.setPosition(rect.left - offsetX, rect.top - offsetY);
			dx = 0;
		}

		void Collision(int dir)
		{
			for (int i = rect.top / 32; i < (rect.top + rect.height) / 32; i++)
				for (int j = rect.left / 32; j < (rect.left + rect.width) / 32; j++)
				{
					if (TileMap[i][j] == 'B' || TileMap[i][j] == 'S')
					{
						
						if ((dx > 0) && (dir == 0)) rect.left = j * 32 - rect.width;
						if ((dx < 0) && (dir == 0)) rect.left = j * 32 + 32;
						if ((dy > 0) && (dir == 1)) { rect.top = i * 32 - rect.height; dy = 0; onGround = true; }
						if ((dy < 0) && (dir == 1)) { rect.top = i * 32 + 32; }			    
					}

					if (TileMap[i][j] == '0')
					{
						TileMap[i][j] = ' ';
						point += 2;
					}

					if (TileMap[i][j] == '1')
					{
						TileMap[i][j] = ' ';
						point += 5;
						
					}
					if(TileMap[i][j] == '2')
					{
						TileMap[i][j] = ' ';
						point += 10;
					}
				}
		}
};

//class ENEMY
//{
//	public:
//		float dx, dy;
//		FloatRect rect;
//		bool onGround;
//		Sprite s;
//		float dvi;
//		bool life;
//
//		void set(Texture &image, int x, int y)
//		{
//			s.setTexture(image);
//			s.setColor(Color::Red);
//			rect = FloatRect(x, y, 25, 30);
//			dx = -0.05;
//			dy = 0;
//			dvi = 0;
//			life = true;
//		}
//
//		void update(float time)
//		{
//			rect.left += dx * time;
//		
//			Collision(0);
//
//			if (!onGround) dy = dy + 0.0011 * time;
//			rect.top += dy * time;
//			onGround = false;
//
//			Collision(1);
//
//			dvi += time * 0.005;
//			if (dvi > 2) dvi -= 2;
//			s.setTextureRect(IntRect(0, 0, 25, 30));
//			if (!(life)) s.setTextureRect(IntRect(100, 0, 25, 30));
//
//			s.setPosition(rect.left - offsetX, rect.top - offsetY);
//				
//		}
//
//		void Collision(int dir)
//		{
//			for (int i = rect.top / 16; i < (rect.top + rect.height) / 16; i++)
//				for (int j = rect.left / 16; j < (rect.left + rect.width) /16; j++)
//				{
//					if (TileMap[i][j] == 'B')
//					{
//						if ((dx > 0) && (dir == 0))
//						{
//							rect.left = j * 16 - rect.width;
//							dx = -0.05;
//						}
//						else if ((dx < 0) && (dir == 0))
//						{
//							rect.left = j * 16 + 16;
//							dx = 0.05;
//
//						}
//						if ((dy > 0) && (dir == 1)) { rect.top = i * 16 - rect.height; dy = 0; onGround = true; }
//						if ((dy < 0) && (dir == 1)) { rect.top = i * 16 + 16; }
//					}
//				}
//			}
//};

int main()
{
	RenderWindow window(VideoMode(600, 400), "Test");
	
	Texture r;
	r.loadFromFile("dvi.png");
	Texture stena;
	stena.loadFromFile("kamen.png");
	Texture O;
	O.loadFromFile("0.png");
	Texture A;
	A.loadFromFile("1.png");
	Texture prig;
	prig.loadFromFile("prig.png");
	Texture chel;
	chel.loadFromFile("chel.png");
	Texture moneta;
	moneta.loadFromFile("moneta.png");

	Font font;
	font.loadFromFile("arial.ttf");
	Text text;
	

	text.setFont(font);
	text.setCharacterSize(18);
	text.setPosition(32, 32);
	text.setColor(Color::Red);

	MONETA mon(moneta);
	PLAYER p(chel);
	p.s.setPosition(40 * 32, 4 * 32);
	/*ENEMY enemy;
	enemy.set(r, 22 * 16, 4 * 16);*/

	float dvi = 0;

	RectangleShape rectangle;
	

	Clock clock;

	while (window.isOpen())
	{
		Event event;
		while (window.pollEvent(event))
		{
			if (event.type == Event::Closed)
				window.close();
		}

		float time = clock.getElapsedTime().asMicroseconds();
		clock.restart();

		time = time / 800;

		std::ostringstream str;
		str << "Score: ";
		str << point;
		text.setString(str.str());

		if (Keyboard::isKeyPressed(Keyboard::A))
		{
			p.dx=-0.1;
		}

		if (Keyboard::isKeyPressed(Keyboard::D))
		{
			p.dx=0.1;
		}
	
		if (Keyboard::isKeyPressed(Keyboard::W))
		{
			if (p.onGround)
			{
				p.dy=-0.4;
				p.onGround=false;
			}
		}

		p.update(time);
		mon.update(time);
		mon.s.setPosition(0, 0);
		/*enemy.update(time);

			if(enemy.life)
			{
				if(p.rect.intersects(enemy.rect))
					if(p.dy > 0)
					{
						enemy.dx = 0; p.dy = -0.2; enemy.life = false;
					}
					else p.s.setColor(Color::Blue);
			
			}*/

		window.clear(Color::White);

		if (p.rect.left > 300) offsetX = p.rect.left - 600 / 2;
		if (p.rect.top > 200) offsetY = p.rect.top - 400 / 2;
		
		for (int i = 0; i < H; i++)
			for (int j = 0; j < W; j++)
			{
				if (TileMap[i][j] == 'B')
				{
					rectangle.setTexture(&stena);
					rectangle.setPosition(j * 32 - offsetX, i * 32 - offsetY);
					rectangle.setSize(Vector2f(32.0, 32.0));
					window.draw(rectangle);
				}

				else if (TileMap[i][j] == '0')
				{
					rectangle.setTexture(&O);
					rectangle.setPosition(j * 32 - offsetX, i * 32 -offsetY);
					rectangle.setSize(Vector2f(32.0, 32.0));
					window.draw(rectangle);
				}

				else if (TileMap[i][j] == '1')
				{	
					rectangle.setTexture(&A);
					rectangle.setPosition(j * 32 - offsetX, i * 32 - offsetY);
					rectangle.setSize(Vector2f(32.0, 32.0));
					window.draw(rectangle);
				}

				else if (TileMap[i][j] == 'S')
				{
					rectangle.setTexture(&prig);
					rectangle.setPosition(j * 32 - offsetX, i * 32 - offsetY);
					rectangle.setSize(Vector2f(32.0, 32.0));
					window.draw(rectangle);
				}
				else if (TileMap[i][j] == '2')
				{
					MONETA m1(mon);
					m1.s.setPosition(j * 32 - offsetX, i * 32 - offsetY);
					window.draw(m1.s);
				}
			}

		window.draw(text);		
		/*window.draw(enemy.s);	*/
		window.draw(p.s);
		window.draw(mon.s);
		window.display();
	}

	return 0;
}